package app.texas.com.devilfishhouse.weight.popbutton.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;



import java.util.ArrayList;
import java.util.List;

import app.texas.com.devilfishhouse.Beans.OfflineChildBean;
import app.texas.com.devilfishhouse.R;


/**
 * 自定义的弹出框列表适配器,类似于大众点评或美团,如果想要此种效果可以直接使用
 * Created by Chris on 2014/12/8.
 */
public class PopupAdapterOffTwo extends BaseAdapter {
    private int resource;
    private int normalBg;
    private int pressBg;
    private int selection;
    private Context mContext;

    public PopupAdapterOffTwo(Context mContext) {
        this.mContext = mContext;
    }

    private List<OfflineChildBean> mItems = new ArrayList<>();


    public void setdate(List<OfflineChildBean> datas) {
        mItems.clear();
        mItems.addAll(datas);
        notifyDataSetChanged();
    }


    private void initParams(int resource, int normalBg, int pressBg) {
        this.resource = resource;
        this.normalBg = normalBg;
        this.pressBg = pressBg;
        this.selection = -1;
    }

    @Override
    public int getCount() {
        return mItems.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        String s = mItems.get(position).getName();
        View view;
        ViewHolder holder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.popup_item, null);
            holder = new ViewHolder();
            holder.tv = (TextView) view.findViewById(R.id.tv);
            view.setTag(holder);
        } else {
            view = convertView;
            holder = (ViewHolder) view.getTag();
        }
        holder.tv.setText(s);
        if (position == selection) {
            holder.tv.setBackgroundResource(pressBg);
        } else {
            holder.tv.setBackgroundResource(normalBg);
        }
        return view;
    }

    public void setPressPostion(int position) {
        this.selection = position;
    }

    class ViewHolder {
        TextView tv;
    }
}
